package com.kfc.sales;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Vector;





import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.net.ParseException;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

public class TopSeller extends ListActivity{
	private LayoutInflater mInflater;
	private Vector<RowData> data;
	RowData rd;
	static final String[] branchloc = new String[] {
	    "Quezon City", "Taft Ave.",	"Mall of Asia","Robinsons Ermita", "Harrison Plaza", "SM Manila", "Binondo", "Tondo", "Recto", "Cavite"    	};


static final String[] branchid = new String[] {
		"2", "53", "132", "26", "3", "19", "132", "72", "65", "12"	};

static final String[] sales = new String[] {
	"12345", "12345", "12345", "12345", "12345", "12345", "12345", "12345", "12345", "12345"	};


TextView tsheader;
Calendar currentDate = Calendar.getInstance();
SimpleDateFormat formatter= new SimpleDateFormat("MMM/dd/yyyy");
String dateNow = formatter.format(currentDate.getTime());

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.topseller);

        tsheader = (TextView) findViewById(R.id.topsellerheader);
        tsheader.setText("Top Sellers as of: "+dateNow);
		mInflater = (LayoutInflater) getSystemService(
				Activity.LAYOUT_INFLATER_SERVICE);
			
			data = new Vector<RowData>();
			
			for(int i=0;i<branchloc.length;i++){
				try {
				 	rd = new RowData(branchloc[i],branchid[i],sales[i]);
				    } catch (ParseException e) {
				    	e.printStackTrace();
				   }
				   data.add(rd);
			}
				   
		ListView list=getListView();
					
					CustomAdapter adapter = new CustomAdapter(this, R.layout.lists,
					                                     R.id.branchloc, data);
					
					setListAdapter(adapter);
					getListView().setTextFilterEnabled(true);
        
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id){  
        super.onListItemClick(l, v, position, id);
        Intent i = new Intent(v.getContext(),ViewCompany.class);
        rd = data.get(position);
        i.putExtra("BranchID", rd.mBranchid);
        i.putExtra("BranchLoc", rd.branchLocation);
        i.putExtra("TotalSales", rd.mSales);
        startActivity(i);
		
	}





    public class RowData {
	       protected String branchLocation;
	       protected String mBranchid;
	       protected String mSales;
	       
	       RowData(String location,String brid,String brsales)
	       {
		       branchLocation=location;
		       mBranchid = brid;
		       mSales=brsales; 
	       }
	
	}
	  
    public class CustomAdapter extends ArrayAdapter<RowData> {
	  public CustomAdapter(Context context, int resource,
	                        int textViewResourceId, List<RowData> objects) {               
	 super(context, resource, textViewResourceId, objects);
	}
	      @Override
	       public View getView(int position, View convertView, ViewGroup parent) {   
	       ViewHolder holder = null;
	       TextView brloc = null;
	       TextView brnchid=null;
	       TextView brnchsale = null;
	       RowData rowData= getItem(position);
	       if(null == convertView){
	            convertView = mInflater.inflate(R.layout.lists, null);
	            holder = new ViewHolder(convertView);
	            convertView.setTag(holder);
	        }
	             holder = (ViewHolder) convertView.getTag();
	             brloc = holder.getLocation();
	             brloc.setText("Branch Location: \n"+rowData.branchLocation);                                                     
	             brnchid=holder.getID();
	             brnchid.setText("Branch ID: \n"+rowData.mBranchid);
	             brnchsale = holder.getSales();
	             brnchsale.setText("Total Sales: "+rowData.mSales);    
	             return convertView;
	       }
	            private class ViewHolder {
	            private View mRow;
	            private TextView location = null;
	            private TextView id=null; 
	            private TextView brsales = null;
	            public ViewHolder(View row) {
	            mRow = row;
	 }
	         public TextView getLocation() {
	             if(null == location){
	                 location = (TextView) mRow.findViewById(R.id.branchloc);
	                }
	            return location;
	         }     
	         public TextView getID() {
	             if(null == id){
	            	 id = (TextView) mRow.findViewById(R.id.branchid);
	                    }
	           return id;
	         }
	        public TextView getSales() {
	             if(null == brsales){
	            	 brsales = (TextView) mRow.findViewById(R.id.totalsales);
	                                      }
	                return brsales;
	        }
	     }
	   }

}
